package leader.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import beanDao.MapToBean;

import teacher.bean.Paper;
import teacher.bean.Teacher;
import tools.StringDate;
import database.dao.MysqlCrud;

public class DirectPaperService {
	/**
	 * @category 查询同一研究方向的所有期刊论文
	 * @author chenkai
	 * @param teacher Teacher 研究方向信息
	 * @return lists List<Paper> 返回以List<Paper>的论文集合
	 */
	public List<Paper> selectAllDirectPaper(Teacher teacher) {
		List<Paper> lists = new ArrayList<Paper>();
		String sql="select * from (select * from paper order by state,type,jclass,meetClass,include,date desc) as acheve ,teacher as T where acheve.teaId = T.teaId and T.direction=? and acheve.state<>? ";
		String[] parameter={
				String.valueOf(teacher.getDirection()),
				String.valueOf(1)
				};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list= mysqlcurd.SelectData(sql, parameter);
		for(int i=0;i<list.size();i++) {
			Map map=list.get(i);
			lists.add(new MapToBean().MapToPaper(map));
		}
		return lists;
	}
	
	/**
	 * @category 查询同一研究方向的所有期刊论文
	 * @author chenkai
	 * @param teacher Teacher 研究方向信息
	 * @return lists List<Paper> 返回以List<Paper>的论文集合
	 */
	public List<Paper> selectSegregateAllDirectPaper(Teacher teacher,int pageNow,int pageSize) {
		List<Paper> lists = new ArrayList<Paper>();
		String sql="select * from (select * from paper order by state,type,jclass,meetClass,include,date desc) as acheve ,teacher as T where acheve.teaId = T.teaId and T.direction=? and acheve.state<>? and acheve.papId LIMIT " +
				""+(pageNow-1)+" ,"+pageSize+"";
		String[] parameter={
				String.valueOf(teacher.getDirection()),
				String.valueOf(1)
				};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list= mysqlcurd.SelectData(sql, parameter);
		for(int i=0;i<list.size();i++) {
			Map map=list.get(i);
			lists.add(new MapToBean().MapToPaper(map));
		}
		return lists;
	}
	
	/**
	 * @category 通过名称模糊查询一个研究方向的一篇期刊论文
	 * @param vague 查询的期刊论文的名称
	 * @param leader 传入的研究方向
	 * @return treatiseOne List<Paper> 返回一个List<Paper>的期刊论文集合
	 */
	public List<Paper> selectPaperLikeName(String vague,Teacher leader) {
		String sql= "select * from (select * from paper order by state,type,jclass,meetClass,include,date desc) as acheve,teacher as T where acheve.teaId=T.teaId and acheve.name like '%"+vague+"%' and T.direction=?and acheve.state<>? ";
		String[] parameter={
				String.valueOf(leader.getDirection()),
				String.valueOf(1)
				};
		List<Paper> lists=new ArrayList<Paper>();
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list = mysqlcurd.SelectData(sql,parameter);
		//System.out.println(list.size());
		for(int i=0;i<list.size();i++) {
			Map map=list.get(i);
			lists.add(new MapToBean().MapToPaper(map));
		}
		return lists;
	}
	
	/**
	 * @category 通过名称模糊查询一个研究方向的一篇期刊论文
	 * @param vague 查询的期刊论文的名称
	 * @param leader 传入的研究方向
	 * @return treatiseOne List<Paper> 返回一个List<Paper>的期刊论文集合
	 */
	public List<Paper> selectSegregatePaperLikeName(String vague,Teacher leader,int pageNow,int pageSize) {
		String sql= "select * from (select * from paper order by state,type,jclass,meetClass,include,date desc) as acheve,teacher as T where acheve.teaId=T.teaId and acheve.name like '%"+vague+"%' and T.direction=?and acheve.state<>?  and papId LIMIT " +
				""+(pageNow-1)+" ,"+pageSize+"";
		String[] parameter={
				String.valueOf(leader.getDirection()),
				String.valueOf(1)
				};
		List<Paper> lists=new ArrayList<Paper>();
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list = mysqlcurd.SelectData(sql,parameter);
		//System.out.println(list.size());
		for(int i=0;i<list.size();i++) {
			Map map=list.get(i);
			lists.add(new MapToBean().MapToPaper(map));
		}
		return lists;
	}
	/**
	 * @category 通过时间段查询方向期刊论文
	 * @param date1 时间段的下限
	 * @param date2 时间段的上限
	 * @param leader 查询的方向
	 * @return PaperOne List<Paper> 返回查询到的List<Paper>论文列表
	 */
	public List<Paper> selectDirectPaperByTime(Date date1,Date date2,Teacher leader) {
		String sql= "select * from (select * from paper order by state,type,jclass,meetClass,include,date desc) as acheve,teacher as T where acheve.teaId=T.teaId and date>='"+date1+"' and date<='"+date2+"' and T.direction=? and acheve.state<>?";
		String[] parameter={
				String.valueOf(leader.getDirection()),
				String.valueOf(1)
				};
		List<Paper> lists=new ArrayList<Paper>();
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list = mysqlcurd.SelectData(sql, parameter);
		//System.out.println(list.size());
		for(int i=0;i<list.size();i++) {
			Map map=list.get(i);
			lists.add(new MapToBean().MapToPaper(map));
		}
		return lists;
	}
	/**
	 * @category 通过时间段查询方向期刊论文
	 * @param date1 时间段的下限
	 * @param date2 时间段的上限
	 * @param leader 查询的方向
	 * @return PaperOne List<Paper> 返回查询到的List<Paper>论文列表
	 */
	public List<Paper> selectSegregateDirectPaperByTime(Date date1,Date date2,Teacher leader,int pageNow,int pageSize) {
		String sql= "select * from (select * from paper order by state,type,jclass,meetClass,include,date desc) as acheve,teacher as T where acheve.teaId=T.teaId and date>='"+date1+"' and date<='"+date2+"' and T.direction=? and acheve.state<>? and papId LIMIT " +
				""+(pageNow-1)+" ,"+pageSize+"";
		String[] parameter={
				String.valueOf(leader.getDirection()),
				String.valueOf(1)
				};
		List<Paper> lists=new ArrayList<Paper>();
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list = mysqlcurd.SelectData(sql, parameter);
		//System.out.println(list.size());
		for(int i=0;i<list.size();i++) {
			Map map=list.get(i);
			lists.add(new MapToBean().MapToPaper(map));
		}
		return lists;
	}
	
	/**
	 * @category 查询同一研究方向的所有期刊论文
	 * @author chenkai
	 * @param teacher Teacher 研究方向信息
	 * @return lists List<Paper> 返回以List<Paper>的论文集合
	 */
	public List<Paper> selectSegregateDirectPaperByState(Teacher teacher,Paper pap,int pageNow,int pageSize) {
		List<Paper> lists = new ArrayList<Paper>();
		String sql="select * from (select * from paper order by state,type,jclass,meetClass,include,date desc) as acheve ,teacher as T where acheve.teaId = T.teaId and acheve.state = ? and T.direction=? and acheve.state<>? and  papId LIMIT " +
				""+(pageNow-1)+" ,"+pageSize+"";
		String[] parameter={
				String.valueOf(pap.getState()),
				String.valueOf(teacher.getDirection()),
				String.valueOf(1)
				};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list= mysqlcurd.SelectData(sql, parameter);
		for(int i=0;i<list.size();i++) {
			Map map=list.get(i);
			lists.add(new MapToBean().MapToPaper(map));
		}
		return lists;
	}
	
	/**
	 * @category 查询同一研究方向的所有期刊论文
	 * @author chenkai
	 * @param teacher Teacher 研究方向信息
	 * @return lists List<Paper> 返回以List<Paper>的论文集合
	 */
	public List<Paper> selectDirectPaperByState(Teacher teacher,Paper pap) {
		List<Paper> lists = new ArrayList<Paper>();
		String sql="select * from (select * from paper order by state,type,jclass,meetClass,include,date desc) as acheve ,teacher as T where acheve.teaId = T.teaId and acheve.state = ? and T.direction=? and acheve.state<>?";
		String[] parameter={
				String.valueOf(pap.getState()),
				String.valueOf(teacher.getDirection()),
				String.valueOf(1)
				};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list= mysqlcurd.SelectData(sql, parameter);
		for(int i=0;i<list.size();i++) {
			Map map=list.get(i);
			lists.add(new MapToBean().MapToPaper(map));
		}
		return lists;
	}
	/**
	 * @category 论文的审核
	 * @author chenkai
	 * @param paper Paper 传入审核的论文以及审核结果
	 * @return flag boolean
	 */
	public boolean updatePaperState(Paper paper) {
		boolean flag = false;
		String sql="update paper set state = ? where papId = ?";
		String[] parameter={
				String.valueOf(paper.getState()),
				String.valueOf(paper.getPapId())
				};
		MysqlCrud mysqlcurd=new MysqlCrud();
		flag = mysqlcurd.InsertAndUpdateData(sql, parameter);
		return flag;
	}
	
	/**
	 * @category 得到一个方向的所有论文的数量
	 * @author chenkai
	 * @return sum int 教师数量
	 */
	public int getCountOfDirectPaper(Teacher teacher) {
		int sum=0;
		String sql="select count(*) from paper as acheve ,teacher as T where acheve.teaId = T.teaId and T.direction=? and acheve.state<>?";
		String[] parameter={
				String.valueOf(teacher.getDirection()),
				String.valueOf(1)
				};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> lists=mysqlcurd.SelectData(sql,parameter);
		Map map=lists.get(0);
		sum=Integer.parseInt(map.get(0).toString());
		return sum;
	}
	
	/**
	 * @category 得到一个方向的所有论文的数量  时间段
	 * @author chenkai
	 * @return sum int 教师数量
	 */
	public int getCountOfDirectPaperByTime(Teacher teacher,Date date1,Date date2) {
		int sum=0;
		String sql="select count(*) from paper as acheve ,teacher as T where acheve.teaId = T.teaId and T.direction=? and acheve.state<>? and date>='"+date1+"' and date<='"+date2+"'";
		String[] parameter={
				String.valueOf(teacher.getDirection()),
				String.valueOf(1)
				};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> lists=mysqlcurd.SelectData(sql,parameter);
		Map map=lists.get(0);
		sum=Integer.parseInt(map.get(0).toString());
		return sum;
	}
	/**
	 * @category 得到一个方向的所有论文的数量 名字
	 * @author chenkai
	 * @return sum int 教师数量
	 */
	public int getCountOfDirectPaperByName(Teacher teacher,String vague) {
		int sum=0;
		String sql="select count(*) from paper as acheve ,teacher as T where acheve.teaId = T.teaId and T.direction=? and acheve.state<>? and acheve.name like '%"+vague+"%'";
		String[] parameter={
				String.valueOf(teacher.getDirection()),
				String.valueOf(1)
				};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> lists=mysqlcurd.SelectData(sql,parameter);
		Map map=lists.get(0);
		sum=Integer.parseInt(map.get(0).toString());
		return sum;
	}
	
	/**
	 * @category 得到一个方向的所有论文的数量状态
	 * @author chenkai
	 * @return sum int 教师数量
	 */
	public int getCountOfDirectPaperByState(Teacher teacher,Paper pap) {
		int sum=0;
		String sql="select count(*) from paper as acheve ,teacher as T where acheve.teaId = T.teaId and acheve.state = ? and T.direction=?and acheve.state<>? ";
		String[] parameter={
				String.valueOf(pap.getState()),
				String.valueOf(teacher.getDirection()),
				String.valueOf(1)
				};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> lists=mysqlcurd.SelectData(sql,parameter);
		Map map=lists.get(0);
		sum=Integer.parseInt(map.get(0).toString());
		return sum;
	}
}
